home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol05 / 06 / cpumeter / histgram.c < prev    next >
C/C++ Source or Header  |  1990-11-01  |  3KB  |  123 lines

  1. //
  2. // histgram.c - (c) 1990 Chiverton Graphics, Inc.
  3. //
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <memory.h>
  9. #include "histgram.h"
  10.  
  11. HPS   hPS        = NULL ;
  12. HWND  hwndClient = NULL ;
  13. HHEAP hHeap      = NULL ;
  14.  
  15. INT   xClientWidth  = 0 ,
  16.       yClientHeight = 0 ;
  17.  
  18. INT   indexMax,
  19.       index,
  20.       iArraySize,
  21.       *npArray;
  22.  
  23.  
  24. VOID create_histogram (HAB hab, HWND hwndClientWindow)
  25.      {
  26.      HDC    hdc   ;
  27.      SIZEL  sizel = { 0L, 0L};
  28.      RECTL  rectl = { 0L, 0L, 3000, 3000 } ;
  29.  
  30.      hwndClient = hwndClientWindow ;
  31.      hdc        = WinOpenWindowDC (hwndClient) ;
  32.      hPS        = GpiCreatePS (hab, hdc, &sizel, PU_PELS | GPIT_MICRO | GPIA_ASSOC) ;
  33.  
  34.      GpiSetBackColor (hPS, CLR_WHITE);
  35.      GpiSetColor (hPS, CLR_BLACK);
  36.      GpiSetLineType (hPS, LINETYPE_SOLID) ;
  37.  
  38.      //  Create a heap on the automatic data segment.
  39.      //  Allocate an array of INTs. The array is large enough
  40.      //  to store the bar chart for a maximized client window.
  41.      //
  42.      iArraySize = WinQuerySysValue (HWND_DESKTOP, SV_CXSCREEN);
  43.      hHeap      = WinCreateHeap (0,0, (sizeof(INT)*iArraySize),0,0,0) ;
  44.      npArray    = (PINT) WinAllocMem (hHeap, (sizeof(INT)*iArraySize)) ;
  45.      index      = 0 ;
  46.      indexMax   = iArraySize - 1 ;
  47.      }
  48.  
  49.  
  50. void size_histogram (INT xPixelWidth, INT yPixelHeight)
  51.      {
  52.      if (xPixelWidth < index) index = 0 ;
  53.  
  54.      xClientWidth  = xPixelWidth ;
  55.      yClientHeight = yPixelHeight;
  56.      indexMax      = xPixelWidth - 1 ;
  57.  
  58.      WinInvalidateRect (hwndClient, NULL, FALSE) ;
  59.      }
  60.  
  61.  
  62. void update_histogram (INT iPercentOfLoad)
  63.      {
  64.      POINTL ptl;
  65.      //
  66.      //  If histogram is full, move it 50% to left.
  67.      //
  68.      if (index > indexMax)
  69.           {
  70.           INT iSkipAmount = xClientWidth / 2;
  71.  
  72.           memcpy (npArray, npArray + iSkipAmount, iSkipAmount*sizeof(INT));
  73.           index = iSkipAmount;
  74.           npArray [index++] = iPercentOfLoad ;
  75.  
  76.           // redraw histogram.
  77.           WinInvalidateRect (hwndClient, NULL, FALSE) ;
  78.           return;
  79.           }
  80.  
  81.      // draw the newest bar
  82.      //
  83.      ptl.x = index;
  84.      ptl.y = 0L;
  85.      GpiMove (hPS, &ptl);
  86.      ptl.y = (iPercentOfLoad/100.) * yClientHeight ;
  87.      GpiLine (hPS, &ptl);
  88.  
  89.      npArray [index++] = iPercentOfLoad ;
  90.      }
  91.  
  92.  
  93. void paint_histogram (void)
  94.      {
  95.      INT    i;
  96.      POINTL ptl;
  97.      RECTL  rectl;
  98.  
  99.      if (hPS == NULL) return;
  100.  
  101.      WinBeginPaint (hwndClient, hPS, &rectl) ;
  102.      GpiErase (hPS);
  103.  
  104.      for (i=0; i<index; i++)
  105.           {
  106.           ptl.x = i;
  107.           ptl.y = 0L;
  108.           GpiMove (hPS, &ptl);
  109.           ptl.y = (npArray [i]/100.) * yClientHeight ;
  110.           GpiLine (hPS, &ptl);
  111.           }
  112.  
  113.      WinEndPaint (hPS) ;
  114.      }
  115.  
  116.  
  117.  
  118. VOID destroy_histogram (void)
  119.      {
  120.      GpiDestroyPS (hPS) ;
  121.      WinDestroyHeap (hHeap) ;
  122.      }
  123.